package ltd.latewhale.whalenest.controller;

import ltd.latewhale.whalenest.model.basicDO;
import ltd.latewhale.whalenest.service.basicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基础信息
 */
@RestController
@CrossOrigin //跨域
@RequestMapping("/basic")
@Validated
public class basicController {

    @Autowired
    private basicService basicService;

    //按ID查询单个数据
    @GetMapping("/{id}")
    public basicDO getId(
            @PathVariable Integer id
    ){
        basicDO data = basicService.getById(id);

        if(data == null){
            return null;
        }
        return data;
    }

    //查询全部数据
    @GetMapping("")
    public List<basicDO> getDatas(){

        List<basicDO> datas = basicService.getDatas();

        System.out.println("查询基础信息");

        if(datas == null){
            return null;
        }

        return datas;
    }

    //按ID删除数据
    @DeleteMapping("/{id}")
    public Integer deleteData(
            @PathVariable Integer id
    ){
        Integer data = basicService.deleteData(id);

        if(data == null){
            return null;
        }

        return 200;
    }

    //更改数据
    @PutMapping("")
    public Integer putData(
            @RequestBody @Validated basicDO dto
    ){
        Integer data = basicService.putData(dto);

        if(data == null){
            return null;
        }

        return 200;
    }

    //增加数据
    @PostMapping("")
    public Integer postData(
            @RequestBody @Validated basicDO dto
    ){
        Integer data = basicService.postData(dto);

        if(data == null){
            return null;
        }

        return 200;
    }
}
